
#include "app_net.h"

//{"sysTime2":"2023-03-08 14:27:01","sysTime1":"20230308142701"}
static int sunning_time_parse(const * json_data)
{

	char temp_buff[200];
			
	cJSON *root = NULL; 
	cJSON *sys1 = NULL; 

	//**************************************
	if(!json_data){
		return 0;
	}

	if(strlen(json_data) > 200){
		return 0;
	}
	//**************************************

	log_n("sunningtime=%s\n",json_data);
	//**************************************
#if 1
	root = cJSON_Parse(json_data);
	if(root == NULL){
		return 0;
	}
	
	sys1 = cJSON_GetObjectItem(root,"sysTime1"); 
	if(sys1 == NULL){
		cJSON_Delete(root);
		return 0;
	}
	//**************************************
	memset(temp_buff,0,sizeof(temp_buff));

	memcpy(temp_buff,sys1->valuestring,strlen(sys1->valuestring));
	//**************************************

#if 0
	log_d("temp_buff=%s.\n",temp_buff);

	for(i=0;i<14;i++){
		log_d("temp_buff[%d]=%d\n",i,temp_buff[i]-'0');
	}
#endif

	int32_t nYear  = (temp_buff[0] -'0')*1000 + (temp_buff[1] -'0')*100 + (temp_buff[2]-'0')*10 + (temp_buff[3]-'0');	
	int32_t nMonth = (temp_buff[4] -'0')*10   + (temp_buff[5] -'0');
	int32_t nDay   = (temp_buff[6] -'0')*10   + (temp_buff[7] -'0');

	int32_t nHour  = (temp_buff[8] -'0')*10   + (temp_buff[9] -'0');
	int32_t nMin   = (temp_buff[10]-'0')*10   + (temp_buff[11]-'0');
	int32_t nSec   = (temp_buff[12]-'0')*10   + (temp_buff[13]-'0');

	log_n("nYear   =%d\n",nYear);
	log_n("nMonth  =%d\n",nMonth);
	log_n("nDay    =%d\n",nDay);
	log_n("nHour   =%d\n",nHour);
	log_n("nMin    =%d\n",nMin);
	log_n("nSec    =%d\n",nSec);

	cJSON_Delete(root);
	root = NULL;
#endif

	return 0;

}



static size_t time_respons_callback(void *buffer, size_t size, size_t nmemb, void *usr_data)
{	
	if((size*nmemb) > 200){
		return 0;
	}

	if(buffer){
		sunning_time_parse(buffer);
	}
	

    return size*nmemb;
}


int net_sunning_servr_time(void)
{	
	static CURL *keycurl = NULL;	 
	CURLcode res;	  	
	long	 retcode  = 0;
	CURLcode code ;


	if(keycurl == NULL){
		keycurl = curl_easy_init(); 	
	}
	if(keycurl == NULL){
		log_e("curl easy init error\n");
		return 0;
	}

	curl_easy_setopt(keycurl, CURLOPT_CUSTOMREQUEST, "GET");
	curl_easy_setopt(keycurl, CURLOPT_URL, "http://quan.suning.com/getSysTime.do");
	curl_easy_setopt(keycurl, CURLOPT_WRITEFUNCTION,  time_respons_callback);			
	curl_easy_setopt(keycurl, CURLOPT_WRITEDATA, NULL);

	curl_easy_setopt(keycurl, CURLOPT_TIMEOUT, 15);
	curl_easy_setopt(keycurl, CURLOPT_MAXREDIRS, 1);
	curl_easy_setopt(keycurl, CURLOPT_CONNECTTIMEOUT, 25);

	curl_easy_setopt(keycurl, CURLOPT_SSL_VERIFYPEER, 0); 
	curl_easy_setopt(keycurl, CURLOPT_SSL_VERIFYHOST, 0);
	res = curl_easy_perform(keycurl);
	if(res != CURLE_OK)
	{
		log_n("Failed to get res = %x\n",res); 	
	}	
	else
	{
		code = curl_easy_getinfo(keycurl, CURLINFO_RESPONSE_CODE , &retcode);
		if((code == CURLE_OK) && retcode == 200)
		{
			res  = CURLE_OK;
		}
		else
		{
			res = -1;
			log_n("getinfo error\n");
		}
	}

	curl_easy_cleanup(keycurl); 
	keycurl = NULL;
	
	return 0;
}








